home *** CD-ROM | disk | FTP | other *** search
- From: clamage@Eng.Sun.COM (Steve Clamage)
- Message-ID: <4jbto7$78i@engnews1.Eng.Sun.COM>
- X-Original-Date: 27 Mar 1996 17:26:31 GMT
- Path: in2.uu.net!bounce-back
- Date: 27 Mar 96 20:12:00 GMT
- Approved: fjh@cs.mu.oz.au
- Newsgroups: comp.std.c++
- Subject: Re: Quick questions (but no longer so quick)
- Organization: Sun Microsystems Inc.
- References: <4jajk2$17aq@mule1.mindspring.com>
- Reply-To: clamage@Eng.Sun.COM
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBFAgUBMVmhROEDnX0m9pzZAQGnswF/Q6yOaso4UkuAY7F+p6Tc0ClSOwPWEO3A
- OunLFEsksnBuspExsLBKof15lcFRCEXO
- =VO3e
-
- In article 17aq@mule1.mindspring.com, abell@mindspring.com (Andrew Bell)
- writes:
- >clamage@Eng.sun.com (Steve Clamage) wrote [but much edited by me]:
- >
- >>I assume you mean a class like
- >> class Short { short val; };
- >>with non-virtual member functions added. You can't assume the size and
- >>alignment of the class will be the same as its only data member, and no,
- >>the standard provides no special support for this special case.
- >
- >Seems like if you only have one data member and no virtual functions,
- >there's no reason to arbitrarily restrict it to a greater alignment
- >level, save maybe to make compiler programmer's lives (and maybe
- >debugger programmer's lives) trivially easier.
-
- That isn't the reason. C++ implementations don't exist in a vacuum. They
- have to coexist with platform ABIs (Application Binary Interface, the
- bit-level description of data and function calls). It is common for an ABI
- to specify rules for structured types that are different from the rules
- for simple types, no matter what the contents of the structure. That is,
- all structured types are treated by one set of rules.
-
- If a platform ABI required that all structs be at least 4-byte aligned and
- padded to 4-byte boundaries, but C++ required that struct alignment and
- padding be no stricter than that of the strictest member, you place the C++
- implementor in an impossible position. Either the generated code is
- incompatible with system and 3rd-party libraries, or the compiler violates
- the C++ standard.
-
- Nothing prevents an implementation from implementing class Short above in
- the same way as type short if the ABI allows it, but the point is that you
- can't depend on type Short being either the same as or different from
- type short.
-
- >>The typeids of two types compare equal if and only if they are
- >>the same type. Base and derived classes are not ever the same type.
- >
- >Fair enough, although in this case RTTI forces the creation of an
- >additional vtable that wouldn't normally be necessary. Since the
- >general criterion for RTTI support in a class in the first place is
- >the existence of any virtual functions (and thus a vtable), it doesn't
- >seem like it would be too unreasonable to make classes with identical
- >vtables evaluate to the same thing. Adding an operator to a class
- >(externally) doesn't change its identity; should a non-virtual member
- >function do so?
-
- That's a philosophical question about what is a type. C++ follows the
- common convention that different types are different types. The One-
- Definition Rule says that there is only one definition of a type in
- a program. In addition, the type must be defined in a single declaration,
- not spread out. Some consequences of this consistent set of rules are that
- a derived type is always a different type from the base, and namespace-scope
- functions are not part of any class's type.
-
- Certainly different rules are possible, and other languages have other
- rules. The C++ rules are easy to understand and the consequences are
- easy to predict. Not so if type equivalence depends on subtle distinctions.
-
- ---
- Steve Clamage, stephen.clamage@eng.sun.com
- ---
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-